{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction to Python\n",
    "  \n",
    "Python is the popular programming language, and it's especially great for beginners.\n",
    "\n",
    "With Python, you can build apps for the web, desktop, and command line. \n",
    "\n",
    "Python is one of the best-suited languages for working with data science and Artificial Intelligence (AI), and it's also used to program IoT devices, microcontrollers, and more!\n",
    "\n",
    "Since its introduction by <b style=\"color:blue\">Guido von Rossum in 1990</b>, it has undergone many changes. \n",
    "\n",
    "* **Python 2.0 in 2000**. \n",
    "\n",
    "* **Python 3.0 in 2008**: This version of Python cleaned up many of the inconsistencies in the design of the various releases of Python 2 (often referred to as Python 2.x). However, it was <b style=\"color:blue\">not backward compatible</b>. That meant that most programs written for earlier versions of Python could not be run using implementations of Python 3.0. \n",
    "\n",
    "We use <b style=\"color:blue\">Python 3</b> throughout this course.\n",
    "\n",
    "**Officeal Web Site**: https://www.python.org/\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 The Python programming language\n",
    "\n",
    "Python is a `high-level language`;\n",
    "\n",
    "Two kinds of programs process high-level languages into low-level languages: **compilers and interpreters**\n",
    "\n",
    "People often describe programming languages as either **compiled** or **interpreted.**\n",
    "\n",
    "* **Compiled**: programs are translated into machine language and then executed by hardware; \n",
    "\n",
    "* **Interpreted**: programs are read and executed by a software interpreter.\n",
    "\n",
    "Usually `C/C++` is considered a compiled language.`Python` is considered an interpreted language. But the distinction is not always clear-cut.\n",
    "\n",
    "There are some general differences between compiled and interpreted languages.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  1.1 The compiled  ,statically typed language : C/C++ \n",
    "\n",
    "**The compiled language** \n",
    "\n",
    "https://en.wikipedia.org/wiki/Compiled_language\n",
    "\n",
    ">A compiled language is a programming language whose implementations are typically  <b style=\"color:blue\">compilers(编译器)</b> :translators that generate **machine code** from `source code`\n",
    "\n",
    "A compiler reads the program and translates it completely before the program starts running. \n",
    "\n",
    "* **compiling**: source code is compiled to object code(translate source code from a high-level programming language to machine code)\n",
    "\n",
    "* **linking**: object code is linked to executable file(combines one or more object files into a single executable file)\n",
    "\n",
    "you can **execute** the `executable` file repeatedly without further translation. \n",
    "\n",
    "![compile](./img/compile.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**The statically typed language**\n",
    "\n",
    "In a statically-typed language,you can tell by looking at the program what type each variable refers to.\n",
    "\n",
    "In general, **static** refers to things that happen at **compile** time (while a program is being compiled),\n",
    "\n",
    "For example, in C/C++ you can write a program like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%file ./demo/src/cpp_styped.cpp\n",
    "#include <iostream>\n",
    "using namespace std;\n",
    " \n",
    "int main()\n",
    "{\n",
    "   int a=1;\n",
    "   float b=0.1;\n",
    "   string c=\"Southeast University\";\n",
    "   cout <<a<<endl; \n",
    "   cout <<b<<endl; \n",
    "   cout <<c<<endl; \n",
    "   return 0;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The begin three lines of the program includes type declarations for the variables:`a,b and c`are declared to be integer,float and string, which means can `check` at `compile time` whether the addition operator is legal for this type (it is). The return value is also declared to be an integer.\n",
    "\n",
    "Because of these declarations, the compiler can check whether the values provided have the right type.These checks happen before the program starts executing, so `errors` can be found `earlier`. More importantly, errors can be found in parts of the program that have never run. \n",
    "\n",
    "Furthermore, these checks don’t have to happen at run time, which is one of the reasons compiled languages generally run `faster` than interpreted languages\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1 Compiling source code to the object code(machine coce)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!g++ -c -o ./demo/obj/cpp_styped.o ./demo/src/cpp_styped.cpp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* -c compiling source code to object code\n",
    "* -o output to the file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!dir .\\demo\\obj\\cpp_styped.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Wildcard character**\n",
    "* **asterisk character (`*`)** matches `zero or more` characters.For example, `doc*` matches `doc` and `document` \n",
    "* **question mark(`?`)** matches exactly `one` character. For example, `doc?` matches `docs` ,but not `doc` and `document` "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2 Linking the object code to executable file(combines one or more object files into a single executable file)**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!g++ -o ./demo/bin/cpp_styped ./demo/obj/cpp_styped.o"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!dir .\\demo\\bin\\cpp_styped.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!.\\demo\\bin\\cpp_styped"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  1.2 Special commands in the code cell of jupyter notebook\n",
    "\n",
    "* magic\n",
    "\n",
    "* shell\n",
    "\n",
    "#### 1.2.1  `magic` functions\n",
    "\n",
    "The magic function system provides a series of functions which allow you to control the behavior of IPython itself, plus a lot of system-type\n",
    "features. \n",
    "\n",
    "There are two kinds of magics,\n",
    "\n",
    "* cell-oriented: %%\n",
    "\n",
    "* line-oriented: %  \n",
    "\n",
    "#####  1.2.1.1  Cell magics: `%%`\n",
    "\n",
    "https://ipython.readthedocs.io/en/stable/interactive/magics.html\n",
    "\n",
    "Cell magics are prefixed with a double `%%`, and they are functions that get as an argument not only the rest of the line, but also `the lines below it in a separate argument`.\n",
    "\n",
    "```bash\n",
    "%%file filename\n",
    "```\n",
    "Write the contents of the **CELL** to a file.\n",
    "\n",
    "#####  1.2.1.2  Line magics: `%`\n",
    "\n",
    "Line magicsare prefixed with the `%` character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%ldir"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Below is a list of available magics**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%lsmagic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2.2  Shell command \n",
    "\n",
    "Run the `system command` in the cell\n",
    "\n",
    "```bash\n",
    "!command\n",
    "```\n",
    "\n",
    "![pythonversion](./img/pythonversion.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!python --version"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3 Python: the interpreted ,scripting, dynamically typed language\n",
    "\n",
    "####  An interpreted language\n",
    "\n",
    "https://en.wikipedia.org/wiki/Interpreted_language\n",
    "\n",
    "**An interpreted language** is a type of programming language for which most of its implementations execute instructions directly and freely, without previously compiling a program into machine-language instructions. The **interpreter executes** the program **directly**, translating each statement into a sequence of one or more subroutines, and then into another language (often machine code).\n",
    "\n",
    "An interpreter reads a high-level program and executes it, meaning that it does what the program says. It processes the program a little at a time, alternately reading lines and performing computations. \n",
    "\n",
    "![interpret](./img/interpret.png)\n",
    "\n",
    "Python is considered an interpreted language because Python programs are executed by an interpreter. \n",
    "\n",
    "There are two ways to use the interpreter: \n",
    "\n",
    "* `command-line` mode: interpreter the statement in the Python Shell\n",
    "\n",
    "* `script` mode:  running a program in a file \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####   Python Script \n",
    "\n",
    "You can write `a program in a file` and use the interpreter to execute the contents of the file.\n",
    "\n",
    "Files that contain Python programs have names that end with  <b style=\"color:blue\">.py</b>. \n",
    "\n",
    "Such a file is called <b style=\"color:blue\">a script(脚本)</b>.Python is the scripting language\n",
    "\n",
    "* A Python program is a sequence of `definitions and commands`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%file ./code/python/demo.py\n",
    "a=2\n",
    "b=3\n",
    "c=6\n",
    "print('The Sample Script')\n",
    "print('a*b+c=',a*b+c)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To execute the program, we have to tell the interpreter the name of the script: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!python ./code/python/demo.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  The dynamically typed language \n",
    "https://en.wikipedia.org/wiki/Type_system#Dynamic_type_checking_and_runtime_type_information\n",
    "\n",
    ">Dynamic type checking is the process of verifying the type safety of a program at runtime\n",
    "\n",
    "In a dynamically-typed language, you `don’t always know the type `of a variable until the program is **running.**\n",
    "\n",
    "In general, dynamic refers to things that happen at **run** time (while a program is running).\n",
    "\n",
    "For example,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1233\n",
    "print(a + 11)\n",
    "a = 0.12222\n",
    "print(a + 0.023)\n",
    "a = \"Southeast University\"\n",
    "print(a + \", Naning\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Looking at this code,the variable `a` to be set to the value 1, then to the value `0.1`, then to a string `\"Southeast University\"`.\n",
    "\n",
    "The variable `a` is assigned several times,each time with values with `different` types. Any values that support the addition operator will work; \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Output: print() function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = 1\n",
    "print(\"b = \", b)\n",
    "print(\"2*3.2 =\", 2 * 3.2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Multiple Assignment \n",
    "\n",
    "Python allows<b style=\"color:blue\"> multiple assignment</b>.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y, z = 2, 3, 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# use multiple assignment to swap the bindings of two variables.\n",
    "x, y = y, x\n",
    "print('x =', x)\n",
    "print('y =', y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Comments: `#`\n",
    " \n",
    "Comments are any text to the right of the <b style=\"color:blue\">#</b> symbol and is mainly useful as notes for the reader of the program.\n",
    " \n",
    "Text following the symbol <b style=\"color:blue\"> #</b> is not interpreted by Python.\n",
    " \n",
    "It is one good way to enhance the <b style=\"color:blue\">readability</b> of code\n",
    "\n",
    "\n",
    ">The comment in C/C++\n",
    ">* `block comments` begin with /* and end with */\n",
    ">* `line comments`  begin with `//` and end with the next `newline` character"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.5 Variable "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  Variable Name\n",
    "\n",
    "In Python, `variable names` can contain \n",
    "\n",
    "* uppercase and lowercase `letters, digits`\n",
    "\n",
    "* they `cannot start with a digit` \n",
    "\n",
    "* the special character <b style=\"color:blue\">_</b>\n",
    "\n",
    "* **reserved words** (sometimes called keywords) in Python that have built-in meanings and `cannot be used` as variable names.\n",
    "\n",
    "\n",
    "\n",
    "Python variable names are <b style=\"color:blue\">case-sensitive</b>\n",
    "\n",
    "* e.g., `Julie and julie` are different names.\n",
    "Apt choice of `variable names` plays an important role in enhancing <b style=\"color:blue\">readability</b>.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 The Basic Elements of Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 Numerical Types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python has **four** basic types:\n",
    "\n",
    "* **int**  integers: -3 or 5 or 10002\n",
    "* **float** real numbers： 3.0 or 3.17 or -28.72，scientific notation： 1.6E3\n",
    "* **bool** the Boolean values <b>True</b> and <b>False</b>\n",
    "* <b style=\"color:blue\">None</b> is a type with a single value.\n",
    "  * `None` is just a value that commonly is used to signify `empty`, or `no value here`.\n",
    "\n",
    "\n",
    "The built-in Python function<b style=\"color:blue\"> type</b> can be used to find out the type of an object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(3.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(TRue) # True Only!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f=False  # False Only!\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Var1=None \n",
    "type(Var1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(NONE) # None Only!!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 Arithmetic Operators\n",
    "\n",
    "**Operators** on types *int and float*\n",
    "\n",
    "```python\n",
    "   +  # Addition\n",
    "   -  # Subtraction\n",
    "   *  # Multiplication\n",
    "   /  # Float Division,Returns a float\n",
    "   // # Integer Divisio, Returns the floor integer\n",
    "   %  # Modulus (Remainder)\n",
    "   ** # Exponentiation\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### `Float` division \n",
    "\n",
    "Returns a float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "6/2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### `Integer` division \n",
    "\n",
    "**the floor integer**\n",
    "\n",
    "* returns the floor integer（向下取整) the quotient and ignores the remainder）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "6//2 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "7//2  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Modulus (Remainder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "7%2 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Exponentiation\n",
    "\n",
    "$i**j$ is $i$ raised to the power $j$,that is \n",
    "\n",
    "* $i^j$ \n",
    "\n",
    "If $i$ and $j$ are both of type int, the result is an int."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "2**3 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If either of them is a float, the result is a float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "2.1**3   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "2**3.1  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Compound Assignment Operators\n",
    "\n",
    "Each of the arithmetic operators has a corresponding shorthand assignment counterpart,\n",
    "\n",
    "```python\n",
    " +=, -=, *=, /=, //=, **=, %=\n",
    "```\n",
    "\n",
    "\n",
    "For example i += 1 is the same as i = i + 1.\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=3\n",
    "a+=1\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 Relational Operators\n",
    "\n",
    "The **Relational(comparison) operators** are:\n",
    "```python\n",
    "   == # equal\n",
    "   != # not equal\n",
    "   >  # greater\n",
    "   >= # at least\n",
    "   <  # less\n",
    "   <= # at most\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1==2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1<=2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.4 Logical Operators \n",
    "\n",
    "```python\n",
    "  and, or, not\n",
    "```\n",
    "The operators on type<b style=\"color:blue\"> bool</b> are:\n",
    "    \n",
    "```python\n",
    "  a and b   # True if both a and b are True, and False otherwise.\n",
    "  a or  b   # True if at least one of a or b is True, and False otherwise.\n",
    "  not   a   # True if a is False, and False if a is True.\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "2>1 and 3>2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1>2 or 3>2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "not 1>2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Further Reading\n",
    "\n",
    "* [Built-in Types:Numeric Types — int, float, complex](https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex)\n",
    "  * **complex numbers**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 Strings \n",
    "\n",
    "Objects of type `str` are used to represent strings of characters.\n",
    "\n",
    "String literals are written in a variety of ways:\n",
    "\n",
    "* **Single quotes**: 'abc'(allows embedded double quotes)\n",
    "* **Double quotes**: \"abc\"(allows embedded single quotes).\n",
    "* **Triple quoted**: '''abc''' or  \"\"\"abc\"\"\",(allows `multi-lines`)\n",
    "\n",
    "Triple quoted strings may **span multiple lines** - all associated whitespace will be included in the string literal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'a '"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(\"abc\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dstr=\"abc'abc'\"\n",
    "print(dstr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=\"\"\"\n",
    "     School Of Power Engineeing\n",
    "     Southeast Unicersity\n",
    "     Nanjing,China\n",
    "     \n",
    "     Triple quoted\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "3*4   # The operator *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "3*\"abc\"   # The operator * is overloaded\n",
    "# 3*4 is equivalent to 4+4+4, \n",
    "# the expression 3*'a' is equivalent to 'a'+'a'+'a'."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "3+4 # The operator *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'a'+'a1'  # The operator * is overloaded"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a =3.0 \n",
    "a# name only, not bound to any object, not a literal of any type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'a'*'a'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The <b>type checking in Python</b> is not as, strong as in some other programming languages (e.g., Java)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'4' < 3  # all numeric values should be less than all values of type str :Python2, not python3!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Strings are one of several <b style=\"color:blue\">sequence types</b> in Python** \n",
    "\n",
    "They <b style=\"color:blue\">share</b> the following operations with `all sequence` types\n",
    "\n",
    "* the <b style=\"color:blue\">length</b> of a string: using the `len(str)` function. \n",
    "* <b style=\"color:blue\">Indexing</b> can be used to extract individual characters from a string. \n",
    "   * all indexing is **zero-based**\n",
    "* <b style=\"color:blue\">Slicing(分片）</b> is used to extract `substrings` of arbitrary length. \n",
    "   * **s[i:j]** :the substring between `i`  to <b style=\"color:blue\">j-1</b>. **NOT j** \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the len(str) function. \n",
    "len('abcd')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# all indexing is zero-based\n",
    "'abcd'[0]\n",
    "str1='abcd'\n",
    "str1[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#all indexing is zero-based\n",
    "'abcd'[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " # the substring i to j-1 \n",
    "'abcd'[1:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 Strings are `immutable`\n",
    "\n",
    "The `String` **cannot be modified `after they are created`**.\n",
    "\n",
    "If you try to modify a character in a string, \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "greeting = 'Hello, world!'# 建立了一个字符串they are created.\n",
    "greeting[0] = 'J' # try to modify the greeting "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You get an **error**\n",
    "```\n",
    "TypeError: 'str' object does not support item assignment\n",
    " ``` \n",
    "The reason for the error is that `strings are immutable`, which means \n",
    "\n",
    "* you can’t **change** an existing string \n",
    "\n",
    "The best you can do is `create a new string` that is a variation on the original:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_greeting = 'J' + greeting[1:]\n",
    "new_greeting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example `concatenates(+)` new first letter onto a slice of greeting. It has no effect on the original string."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 Advanced String Slicing\n",
    "\n",
    "#### 3.2.1 Omit the starting/ending index\n",
    "\n",
    "you can slice a string with a call such as <b style=\"color:blue\">s[i:j]</b>, which gives you a portion of string s from index <b style=\"color:blue\">i</b> to index <b style=\"color:blue\">j-1</b>. \n",
    "\n",
    "However this is not the only way to slice a string! \n",
    "\n",
    "* omit the <b style=\"color:blue\">starting index</b>, start the slice at index <b style=\"color:blue\">0</b>. \n",
    "\n",
    "* omit the <b style=\"color:blue\">ending index</b>, end the slice at <b style=\"color:blue\">the end of the string</b>. \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = 'Python is Fun!'\n",
    "s[1:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the omitted starting index is 0,\n",
    "s[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# the omitted ending index, end the slice at the end of the string.is 0,\n",
    "s[1:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " **original string**\n",
    " \n",
    " If you `omit` both the `start and ending` index, you get your `original string`!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get the original string\n",
    "s[:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.2.2 The slicing step\n",
    "\n",
    "You can add a third parameter,<b style=\"color:blue\"> k</b>, \n",
    "\n",
    "```python\n",
    "s[i:j:k]\n",
    "```\n",
    "\n",
    "This gives a slice of the string s from index i to index j-1, with <b style=\"color:blue\">step size k</b>. \n",
    "\n",
    "Check out the following examples:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = 'Python is Fun!'\n",
    "s[1:12:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s[1:12:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**every `other` character**\n",
    "\n",
    "step=2\n",
    "\n",
    "With ```s[::2]```, we're asking for the `full string s` (from index 0 through 13), with a `step size of 2`, so we end up with every **other** character> in s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s[::2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**reversing a string**\n",
    "\n",
    " step=-1\n",
    " \n",
    "* the negative step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = 'Python is Fun!'\n",
    "s[::-1]  #  the negative step"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.2.3 positive and negative indexs\n",
    "\n",
    "See the Figure for a helpful illustration of slicing with `positive and negative` integers.\n",
    "![slicing](./img/Python-Slicing.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s='HELLO!'\n",
    "s[2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s[-5:-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s[1:5:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Please try the slicing in the follow cell**\n",
    "\n",
    "<table class=\"table-zebra\" style=\"width:60%\">\n",
    "  <thead>\n",
    "  <tr>\n",
    "    <th>Operator</th>\n",
    "    <th>Usage</th>\n",
    "    <th>Description</th>\n",
    "    <th>Examples <span class=\"font-code\">s = 'Hello'</span></th>\n",
    "  </tr>\n",
    "  </thead>\n",
    "  <tbody class=\"font-code\">\n",
    "  <tr class=\"tr-alt\">\n",
    "    <td style=\"text-align:center\"><strong>[<em>i</em>]<br>\n",
    "      [-<em>i</em>]</strong></td>\n",
    "    <td>str[i]<br>\n",
    "      str[-i]</td>\n",
    "    <td class=\"font-normal\">Indexing to get a character.<br>\n",
    "    The front index begins at <code>0</code>; back index begins at <code>-1</code> (<code>=len(<em>str</em>)-1</code>).</td>\n",
    "    <td>s[1] ⇒ 'e'<br>\n",
    "    s[-4] ⇒ 'e'</td>\n",
    "  </tr>\n",
    "  <tr>\n",
    "    <td style=\"text-align:center\"><strong>[<em>m</em>:<em>n</em>:<em>step</em>]<br>\n",
    "    [<em>m</em>:<em>n</em>]<br>\n",
    "      [<em>m</em>:]<br>\n",
    "      [:<em>n</em>]<br>\n",
    "      [:]\n",
    "    </strong></td>\n",
    "    <td>str[m:n:step]<br>\n",
    "      str[m:n]<br>\n",
    "      str[m:]<br>\n",
    "      str[:n]<br>\n",
    "      str[:]</td>\n",
    "    <td class=\"font-normal\">Slicing to get a substring.<br>From index <code>m</code> (included) to <code>n</code> (excluded) with <code>step</code> size.<br>\n",
    "    The defaults are: <code>m=0</code>, <code>n=-1</code>, <code>step=1</code>.</td>\n",
    "    <td>s[1:3] ⇒ 'el'<br>s[1:-2] ⇒ 'el'<br>s[3:] ⇒ 'lo'<br>s[:-2] ⇒ 'Hel'<br>s[:] ⇒ 'Hello'<br>s[0:5:2] ⇒ 'Hlo'</td>\n",
    "  </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s='Hello'\n",
    "s[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 The special string literals\n",
    "\n",
    "#### Eescape character \n",
    "\n",
    "Eescape character begins with a backslash`\\`\n",
    "\n",
    "You use to inform that the next character is special.\n",
    "\n",
    "\n",
    "|Escape Sequence| Control Character|\n",
    "| ---- |:----------:|\n",
    "|\\n |Newline|\n",
    "|\\t| Horizontal tab|\n",
    "|\\v |Vertical tab|\n",
    "|\\b| Backspace|\n",
    "|\\r |Carriage return|\n",
    "|\\f |Form feed|\n",
    "|\\a| Alert/bell|\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('escape character\\n') \n",
    "print('\\t escape character')\n",
    "print('\\tescape character')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  *r'string'* raw string literals \n",
    "\n",
    "**r'string'** raw string literals produce a string just like a normal string literal does"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# r'' raw string literals produce a string just like a normal string literal does。\n",
    "print(r'escape character\\n')\n",
    "print(r'\\t escape character')\n",
    "print(r'\\tescape character')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4 Input & Type Conversion\n",
    "\n",
    "\n",
    "### 4.1 Input\n",
    "Python 3 has a function, `input`, that can be used to get input directly from a user.It takes a string as an argument and displays it as a prompt in the shell. It then waits for the user to type something, followed by hitting the enter key. \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = input('Enter your name: ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Are you really', name, '?')  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b style=\"color:blue\">The line typed by the user treated as a string</b> and becomes the value returned by the function.\n",
    "\n",
    "if you type 3,the variable `n` is bound to the **string** `'3'` not the **int** `3`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = input('Enter an int: ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(n)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n+1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "if  you want to get integer 3,you must conversion string ri integer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n=int(n)\n",
    "n+1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.2 Type Conversion\n",
    "\n",
    "Type conversions(also called type casts) are used often in Python code.  \n",
    "\n",
    "We use **the name of a type** to convert values to that type.\n",
    "\n",
    "* `int(x)` converts x to an integer. \n",
    "\n",
    "* `float(x`) converts x to a floating-point number. \n",
    "\n",
    "* `str(x)` converts object x to a string representation. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n=\"2\"\n",
    "a=int(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When `a float` is converted to an `int`, the number is <b style=\"color:blue\">truncated</b> (not rounded), e.g., "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "number=3.1\n",
    "int(number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "int(\"2.3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "float(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str(2.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str(123)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Python**: the interpreted ,dynamically typed, scripting Language\n",
    "   * Variables and Assignment\n",
    "   * Comments：`#`Output: `print()`\n",
    "   * Numerical Types, Operators and Expressions \n",
    "   * String, Slicing s[i:j] s[i:j:k]\n",
    "   * Input ：input()\n",
    "   * Type Conversion\n",
    "\n",
    "Jupyter\n",
    "  *  `magic` functions：`cell` %%file  , `line`  %ldir \n",
    "  *  `!`  Shell command"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
